package com.zhzh.StudySpringBoot.web;

import com.zhzh.StudySpringBoot.domain.User;
import com.zhzh.StudySpringBoot.domain.UserMapper;
import com.zhzh.StudySpringBoot.domain.UserRepository;
import com.zhzh.StudySpringBoot.service.BlogProperties;
import com.zhzh.StudySpringBoot.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2017/11/8.
 */
@RestController
@RequestMapping(value = "/rest")
public class HelloRestController
{

    @Autowired
    private BlogProperties blogProperties;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserMapper userMapper;

    /**
     * MyBatis
     * @return
     * @throws Exception
     */
    @RequestMapping("/findByName")
    public String findByName() throws Exception {
        User u = userMapper.findByName("xiaomi");
        System.out.println(u.toString());
        //userService.create("xiaomi",1);
        //System.out.println("userRepository:"+userRepository.findByName("xiaomi").getName()+" "+userRepository.findByName("xiaomi").getAge());
        return u.toString();
    }

    /**
     * 使用JdbcTemplate
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryAllUser")
    public String queryAllUser() throws Exception {
        int userNum = userService.getAllUsers();
        System.out.println(userNum);
        //userService.create("xiaomi",1);
        //System.out.println("userRepository:"+userRepository.findByName("xiaomi").getName()+" "+userRepository.findByName("xiaomi").getAge());
        return String.valueOf(userNum);
    }

    /**
     * 使用Spring-data-jpa简化数据访问层
     * @return
     * @throws Exception
     */
    @RequestMapping("/insertUser")
    public String insertUser() throws Exception {
        userService.create("xiaomi",1);
        int userNum = userService.getAllUsers();
        System.out.println(userNum);
        User user = userRepository.findByName("xiaomi");
        //System.out.println("userRepository:"+userRepository.findByName("xiaomi").getName()+" "+userRepository.findByName("xiaomi").getAge());
        return user.toString();
    }

    @RequestMapping("/helloRest")
    public String index() throws Exception {
        //throw new Exception("发生错误");
        System.out.println("zz--------------"+blogProperties.getName());
        System.out.println("zz--------------"+blogProperties.getValue());
        int userNum = userService.getAllUsers();
        System.out.println(userNum);
        //userService.create("xiaomi",1);
        System.out.println("userRepository:"+userRepository.findByName("xiaomi").getName()+" "+userRepository.findByName("xiaomi").getAge());
        return "Hello World";
    }

    @RequestMapping("/indexRest")
    public String index(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        System.out.println("velocity");
        return "index";
    }

    @RequestMapping("/indexTestThymeleafRest")
    public String TestThymeleaf(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        System.out.println("TestThymeleaf");
        return "index";
    }
}
